from fastapi import Depends, APIRouter

from apps.system import cruds, schemas
from apps.system.params.version import VersionParams
from core.auth import Auth, FullAdminAuth, AllUserAuth
from core.dependencies import IdList
from core.response import SuccessResponse

router = APIRouter(prefix="/version")


@router.get("/list", summary="获取版本列表")
async def list_versions(
        params: VersionParams = Depends(),
        auth: Auth = Depends(FullAdminAuth(permissions=["system.version.list"]))
):
    datas, count = await cruds.VersionDal(auth.db).get_datas(**params.dict(), v_return_count=True)
    return SuccessResponse(datas, count=count)


@router.post("/create", summary="创建版本")
async def create_version(
        data: schemas.Version,
        auth: Auth = Depends(FullAdminAuth(permissions=["system.version.create"]))
):
    return SuccessResponse(
        await cruds.VersionDal(auth.db).create_data(data=data),
        msg="创建成功"
    )


@router.put("/update/{data_id}", summary="更新版本")
async def put_version(
        data_id: int,
        data: schemas.Version,
        auth: Auth = Depends(FullAdminAuth(permissions=["system.version.update"]))
):
    return SuccessResponse(
        await cruds.VersionDal(auth.db).put_data(data_id, data),
        msg="修改成功"
    )


@router.delete("/delete", summary="批量删除版本")
async def delete_version(
        ids: IdList = Depends(),
        auth: Auth = Depends(FullAdminAuth(permissions=["system.version.delete"]))
):
    await cruds.VersionDal(auth.db).delete_datas(ids=ids.ids, v_soft=True)
    return SuccessResponse(msg="删除成功")


@router.get("/options", summary="获取版本选择项")
async def list_version_options(
        auth: Auth = Depends(AllUserAuth())
):
    return SuccessResponse(await cruds.VersionDal(auth.db).get_version_options())
